import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LENOVO
 * Date: 2024-08-14
 * Time: 10:08
 */
//最小的k个数，要建立大顶堆（每次要把最大的弹出去）
class IntCom implements Comparator<Integer> {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}
public class Test {
    public int[] smallList (int[] arr ,int k){
        if(arr.length == 0 || k == 0){
            return null;
        }
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(k,new IntCom());//默认小根堆
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(arr[i]);
        }
        for (int i = k; i < arr.length; i++) {
            if(arr[i] < priorityQueue.peek()){
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }
    //----------------------------------------
    //直接插入排序
    public static void insertSort(int[] array){
        if(array.length == 1){
            return;
        }
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0 ; j--) {
                if(array[j] > tmp){
                    array[j + 1] = array[j];
                }else{
                    array[j + 1] = tmp;
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    public static void shellSort(int[] array){
        int gap = array.length;
        while(gap > 0){
            gap /= 2;
            shell(array,gap);
        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0 ; j--) {
                if(array[j] > tmp){
                    array[j + gap] = array[j];
                }else{
                    array[j + gap] = tmp;
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[]{10,2,5,7,3};
        insertSort(arr);
        System.out.println(arr.toString());
    }
}
